package cc.lucien.web.controller;

import cc.lucien.web.entity.User;
import cc.lucien.web.service.UserService;
import cc.lucien.web.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/web/user.do")
public class UserServlet extends HttpServlet {

    // 引入业务逻辑层UserService组件
    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "queryAllUsers";
        }
        switch (method) {
            case "queryAllUsers":
                this.queryAllUsers(req, resp);
                break;
            case "deleteById":
                this.deleteById(req, resp);
                break;
            case "addUser":
                this.addUser(req, resp);
                break;
            case "queryById":
                this.queryById(req, resp);
                break;
            case "editUser":
                this.editUser(req, resp);
                break;
            default:
                resp.sendRedirect(req.getServletContext().getContextPath()+"/pages/error.jsp");
        }
    }

    /**
     * 根据id修改一条用户记录
     * @param req
     * @param resp
     */
    private void editUser(HttpServletRequest req, HttpServletResponse resp) {
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String tel = req.getParameter("phone");
        String email = req.getParameter("email");
        String stateStr = req.getParameter("state");
        Integer state = Integer.parseInt(stateStr);
        User user = new User(id, name, username, password, tel, email, state);
        int result = userService.editUser(user);
        if (result > 0) {
            try {
                // 修改成功
                resp.sendRedirect(req.getServletContext().getContextPath()+"/web/user.do");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据id查询一条用户记录
     * @param req
     * @param resp
     */
    private void queryById(HttpServletRequest req, HttpServletResponse resp) {
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        User user = userService.queryById(id);
        if (user != null) {
            try {
                req.setAttribute("user", user);
                req.getRequestDispatcher("/pages/edit.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加一个用户
     * @param req
     * @param resp
     */
    private void addUser(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String tel = req.getParameter("phone");
        String email = req.getParameter("email");
        User user = new User(name, username, password, tel, email, 0);
        int result = userService.addUser(user);
        if (result > 0) {
            // 添加成功
            try {
                resp.sendRedirect(req.getServletContext().getContextPath() + "/web/user.do");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据id删除一个用户
     * @param req
     * @param resp
     */
    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        String idStr = req.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        int result = userService.deleteById(id);
        if (result > 0) {
            // 说明删除成功
            try {
                resp.sendRedirect(req.getServletContext().getContextPath()+"/web/user.do");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询所有用户数据处理方法
     * @param req
     * @param resp
     */
    private void queryAllUsers(HttpServletRequest req, HttpServletResponse resp) {
        List<User> list = userService.queryAllUsers();
        req.setAttribute("userList", list);
        try {
            req.getRequestDispatcher("../pages/list.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
